package com.tao.mvplibrary.mvp.base;


import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.tao.mvplibrary.mvp.IBaseView;
import com.tao.mvplibrary.mvp.IView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.lang.reflect.ParameterizedType;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Administrator on 2019-8-7.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements LifecycleOwner, IBaseView<P> {
    P mPresenter;
    public View mView;
    public Context mContext;
    private Unbinder bind;
    private IView attachView;
    boolean allowRemove = false;

    public static <T extends BaseFragment> T getInstance(Class<T> tClass) throws Exception {
        return tClass.newInstance();
    }

    public BaseFragment() {
        super();
        if (getArguments() == null)
            setArguments(new Bundle());
    }

    public void setAllowRemove(boolean allowRemove) {
        this.allowRemove = allowRemove;
    }

    public boolean isAllowRemove() {
        return allowRemove;
    }

    public View mContextView = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
//        Lg.e(this, "onCreate " + (mContextView == null));
        try {
            attachView = getAttachView();
            getP().attachView(attachView);
        } catch (Exception e) {
//            e.printStackTrace();
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if (null == mContextView) {
            View mView = bindView(inflater.getContext());
            if (null == mView) {
                mContextView = inflater.inflate(getLayoutId(), container, false);
            } else {
                mContextView = mView;
            }
            bind = ButterKnife.bind(this, mContextView);

            initSomethingAfterBindView();
        }
        mContextView.setClickable(true);
//        Lg.e(this, "onCreateView " + mContextView);

        return mContextView;
    }

    private void initSomethingAfterBindView() {
    }

    protected View bindView(Context context) {
        return null;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mView = view;
        mContext = view.getContext();
        initView(mContextView);
        try {
            getP().setContext(mContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public Context getContext() {
        if (mContext != null) return mContext;
        return super.getContext();
    }

    @Override
    public abstract int getLayoutId();

    @Override
    public void initView(View mContextView) {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
//        Lg.e(this, "onActivityCreated ");

    }

    @Override
    public void initData() {

    }

    public IView getAttachView() {
        return this;
    }

    @Override
    public P getP(IView v) throws Exception {
        if (v == null)
            return getP();

        P presenter = getP();
        if (presenter != null && !presenter.isAttachedV())
            presenter.attachView(v);
        return presenter;
    }


    @Override
    public P getP() throws Exception {
        if (mPresenter == null) {
            //实例化P层，类似于p = new P();
            ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
            Class<P> clazz = (Class<P>) parameterizedType.getActualTypeArguments()[0];
            mPresenter = clazz.newInstance();
        }

        return mPresenter;
    }


    @Override
    public void onResume() {
        super.onResume();
        Lg.e(this, "onResume ");

    }

    @Override
    public void onPause() {
        super.onPause();
        Lg.e(this, "onPause ");

    }

    @Override
    public void onStop() {
        super.onStop();
//        Lg.e(this, "onStop ");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Lg.e(this, "onDestroy ");
        EventBus.getDefault().unregister(this);
    }

    @Subscribe()
    public void onSub(String str) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Lg.e(this, "onHiddenChanged " + hidden);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != mPresenter)
            mPresenter.dettachView();
        if (bind != null)
            bind.unbind();
        bind = null;
//        Lg.e(this, "onDestroyView ");
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        Lg.e(this, "onDetach ");

    }

    public Lifecycle getLifecycle() {
        return super.getLifecycle();
    }

    public static class Lg {


        public static void e(Object... obj) {
            if (obj == null)
                return;
            for (int i = 0; i < obj.length; i++) {
                if (i == 0 && obj.length != 1)
                    continue;
                Log.e(obj[0].getClass().getSimpleName(), "" + obj[i]);
            }
        }
    }
}
